<?php
require_once 'google/apiClient.php';
require_once 'google/contrib/apiDriveService.php';

class DRIVE{
    //-------------------------------------------------
    public function __construct(){
        $client = new apiClient();
        $client->setApplicationName(CLIENT_NAME);
        $client->setClientId(CLIENT_ID);
        $client->setClientSecret(CLIENT_SECRET);
        $client->setRedirectUri(CLIENT_URL);
        $client->setDeveloperKey(CLIENT_API);
        $SCOPES=array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile');
        $client->setScopes($SCOPES);
        $this->service=new apiDriveService($client);
        if (isset($_GET['code'])){$client->authenticate();$_SESSION['token'] = $client->getAccessToken();url(CLIENT_URL);}
        (isset($_SESSION['token']))?$client->setAccessToken($_SESSION['token']):'';
        ($client->getAccessToken())?$_SESSION['token']=$client->getAccessToken():url($client->createAuthUrl());
    }
    //-------------------------------------------------
    public function show(){
        $list=$this->service->files->listFiles();
        $item=$list['items'];
        foreach ($item as $value){
            $name=$value['originalFilename'];
            $file=str_replace('&e=download&gd=true','',$value['downloadUrl']);
            $image=str_replace('s20','s60',$value['thumbnailLink']);
            var_dump($value);
            echo '<br/><br/><br/>';
            //echo '<a href="'.$file.'"><img src="'.$image.'"/></a>';
        }
    }
    //-------------------------------------------------
    function ins($title,$filepath){
        (strtolower(substr($filepath,strlen($filepath)-3,3))=='swf')?$mimetype='application/x-shockwave-flash':$mimetype='application/pdf';
        $file = new DriveFile();
        $file->setTitle($title);
        $file->setMimeType($mimetype);
        if (FOLDER != null){
            $parent = new ParentReference();
            $parent->setId(FOLDER);
            $file->setParents(array($parent));
        }
        try {
            $data=file_get_contents($filepath);
            $createdFile=$this->service->files->insert($file,array('data' => $data,'mimeType' => $mimetype));
            return $createdFile;
        } catch (Exception $e){print "Có lỗi xảy ra: " . $e->getMessage();}
    }
    //-------------------------------------------------
    function del($fileId){
        try {
            $this->service->files->trash($fileId);
        } catch (Exception $e) {print "Có lỗi xảy ra: " . $e->getMessage();}
    }
    //-------------------------------------------------
    function res($fileId) {
        try {
            $this->service->files->untrash($fileId);
        } catch (Exception $e) {print "Có lỗi xảy ra: " . $e->getMessage();}
    }
    //-------------------------------------------------
    function info($fileId){
        try {
            $file = $this->service->files->get($fileId);
            return $file;
        } catch (Exception $e) {print "Có lỗi xảy ra: " . $e->getMessage();}
    }
    //-------------------------------------------------
    function get($filename,$fileID){
        $google='https://docs.google.com/uc?id=';
        $mime='application/pdf';
        SYSTEM::download($filename.'.pdf',$google.$fileID,$mime);
    }
    //-------------------------------------------------
    function url($fileID){
        return 'https://docs.google.com/uc?id='.$fileID;
    }
    //-------------------------------------------------
    function img($fileID,$size){
        $info=$this->info($fileID);
        $image=$info['thumbnailLink'];
        return str_replace('s220','s'.$size,$image);
    }
    //-------------------------------------------------
    function size($fileID){
        $info=$this->info($fileID);
        return round($info['fileSize']/(1024*1024),2);
    }
    //-------------------------------------------------
    function parent($fileID){
        $info=$this->info($fileID);

    }
    //-------------------------------------------------
    function path($fileID){
        $info=$this->info($fileID);
    }
    // Code in here
}